﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReceiptTracker
{
    public partial class FormMain : Form
    {
        private List<string[]> latestMonth = new List<String[]>();
        private SQLiteDB receiptsDB = new SQLiteDB();
        private FormAdvanced FormAdvanced;
        private FormAddReceipt FormAddReceipt;
        private FormAddShop FormAddShop;
        private FormAddCategory FormAddCategory;
        private FormViewReceipts FormViewReceipts;
        private FormViewMonthly FormViewMonthly;
        private FormMontlyAvg FormMonthlyAvg;
        private FormEditReceipt FormEditReceipt;
        private FormEditCategory FormEditCategory;
        private FormEditShop FormEditShop;
        private PieChart piechart;
        public SQLiteDB DB
        {
            get { return receiptsDB; }
            set { receiptsDB = value; }
        }
        public FormMain()
        {
            InitializeComponent();

            //Select dbfile and create all neccessary tables
            receiptsDB.DBFile = "receipts.db";
            receiptsDB.SQLCommand("CREATE TABLE IF NOT EXISTS Receipts(id INTEGER PRIMARY KEY ASC, date TEXT NOT NULL, spent NUMERIC NOT NULL, shop INTEGER NOT NULL);");
            receiptsDB.SQLCommand("CREATE TABLE IF NOT EXISTS Shops(id INTEGER PRIMARY KEY ASC, name TEXT NOT NULL, category INTEGER NOT NULL);");
            receiptsDB.SQLCommand("CREATE TABLE IF NOT EXISTS Categories(id INTEGER PRIMARY KEY ASC, name TEXT NOT NULL);");
            FormAdvanced = new FormAdvanced(DB);
            FormAddReceipt = new FormAddReceipt(DB);
            FormAddShop = new FormAddShop(DB);
            FormAddCategory = new FormAddCategory(DB);
            FormViewReceipts = new FormViewReceipts(DB);
            FormViewMonthly = new FormViewMonthly(DB);
            FormMonthlyAvg = new FormMontlyAvg(DB);
            FormEditReceipt = new FormEditReceipt(DB);
            FormEditCategory = new FormEditCategory(DB);
            FormEditShop = new FormEditShop(DB);
            piechart = new PieChart(this.CreateGraphics(), "Monthly Expenditure", "kr");
            GetLatestMonth();
        }

        private void GetLatestMonth()
        {
            latestMonth.Clear();
            DB.SQLQuery("SELECT ROUND(SUM(spent),2) AS spent,Categories.name FROM Receipts,Shops,Categories WHERE shop=Shops.id AND category=Categories.id AND date>='" +
                 DateTime.Today.AddMonths(-1).ToShortDateString() + "' GROUP BY Categories.name");
            foreach (DataRow row in DB.QueryResult.Rows)
            {
                latestMonth.Add(new string[] {row["spent"].ToString(), row["name"].ToString()});
            }
            this.Invalidate();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void advancedToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            FormAdvanced.Show();
            GetLatestMonth();
        }

        private void receiptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddReceipt.ShowDialog();
            if (FormAddReceipt.DialogResult == DialogResult.OK)
            {
                GetLatestMonth();
            }
        }

        private void shopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddShop.ShowDialog();
        }

        private void receiptsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormViewReceipts.Show();
        }

        private void categoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddCategory.ShowDialog();
        }

        private void monthlyCostsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormViewMonthly.Show();
        }

        private void receiptToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormEditReceipt.ShowDialog();
            if (FormEditReceipt.ValuesChanged)
            {
                GetLatestMonth();
            }
        }

        private void FormMain_Paint(object sender, PaintEventArgs e)
        {
            piechart.ClearItems();
            foreach (string[] item in latestMonth)
            {
                piechart.AddItem(new PieChart.ChartItem(Convert.ToDouble(item[0]), item[1]));
            }
            piechart.DrawChart(25, 50, 300, 300);
        }

        private void monthlyAverageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormMonthlyAvg.Show();
        }

        private void shopToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormEditShop.ShowDialog();
            if (FormEditShop.ValuesChanged)
            {
                GetLatestMonth();
            }
        }

        private void categoryToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormEditCategory.ShowDialog();
            if (FormEditCategory.ValuesChanged)
            {
                GetLatestMonth();
            }
        }
    }
}
